<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拍卖专区</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #fff9e6;
            margin: 0;
            padding: 20px;
        }

        .header {
            max-width: 1200px;
            margin: 0 auto 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            color: #8b4513;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .search-bar {
            display: flex;
            gap: 10px;
            align-items: center;
            flex: 1;
            max-width: 600px;
            margin: 0 20px;
        }

        .search-input {
            flex: 1;
            padding: 10px;
            border: 2px solid #ffd700;
            border-radius: 6px;
            font-size: 14px;
        }

        .search-btn {
            padding: 10px 20px;
            background-color: #ffd700;
            border: none;
            border-radius: 6px;
            color: #8b4513;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .search-btn:hover {
            background-color: #ffb700;
        }

        .category-select {
            padding: 10px;
            border: 2px solid #ffd700;
            border-radius: 6px;
            background-color: white;
            color: #666;
        }

        .auction-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }

        .auction-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }

        .auction-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }

        .auction-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
        }

        .auction-info {
            padding: 15px;
        }

        .auction-title {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }

        .price-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }

        .current-price {
            color: #ff4d4f;
            font-size: 18px;
            font-weight: bold;
        }

        .original-price {
            color: #999;
            text-decoration: line-through;
            font-size: 14px;
        }

        .time-info {
            display: flex;
            align-items: center;
            gap: 5px;
            color: #666;
            font-size: 14px;
        }

        .bid-count {
            color: #ffd700;
            font-weight: bold;
        }

        .status-tag {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            margin-bottom: 10px;
        }

        .status-upcoming {
            background-color: #e6f7ff;
            color: #1890ff;
        }

        .status-ongoing {
            background-color: #f6ffed;
            color: #52c41a;
        }

        .status-ended {
            background-color: #fff1f0;
            color: #ff4d4f;
        }

        .location {
            display: flex;
            align-items: center;
            gap: 5px;
            color: #666;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1><i class="fas fa-gavel"></i> 拍卖专区</h1>
        <div class="search-bar">
            <input type="text" class="search-input" placeholder="搜索拍卖商品...">
            <select class="category-select">
                <option value="">全部分类</option>
                <option value="1">电子产品</option>
                <option value="2">图书教材</option>
                <option value="3">服装饰品</option>
                <option value="4">运动器材</option>
                <option value="5">其他</option>
            </select>
            <button class="search-btn"><i class="fas fa-search"></i></button>
        </div>
        <button class="search-btn" onclick="location.href='publish-auction.html'">
            <i class="fas fa-plus"></i> 发布拍卖
        </button>
    </div>

    <div class="auction-grid" id="auctionList">
        <!-- 拍卖商品卡片将通过JavaScript动态加载 -->
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        // 加载拍卖商品列表
        function loadAuctions() {
            console.log('开始加载拍卖列表...');
            $.ajax({
                url: '/auction/list',
                type: 'GET',
                success: function(response) {
                    console.log('获取拍卖列表响应:', response);
                    if (response.state === 1) {
                        console.log('拍卖列表数据:', response.data);
                        renderAuctions(response.data);
                    } else {
                        console.error('加载拍卖商品失败:', response.msg);
                        alert('加载拍卖商品失败：' + response.msg);
                    }
                },
                error: function(xhr, status, error) {
                    console.error('加载失败:', {
                        status: status,
                        error: error,
                        response: xhr.responseText
                    });
                    alert('加载失败，请稍后重试');
                }
            });
        }

        // 渲染拍卖商品列表
        function renderAuctions(auctions) {
            console.log('开始渲染拍卖列表，数量:', auctions.length);
            const container = $('#auctionList');
            container.empty();

            auctions.forEach(auction => {
                console.log('渲染拍卖商品:', auction);
                const status = getAuctionStatus(auction.startTime, auction.endTime);
                console.log('商品状态:', status);
                
                try {
                    const statusClass = getStatusClass(status);
                    const statusText = getStatusText(status);
                    console.log('商品图片:', auction.firstImage);

                    const card = `
                        <div class="auction-card" onclick="location.href='auction-detail.html?id=${auction.id}'">
                            <img src="${auction.firstImage}" class="auction-image" alt="${auction.productName}" 
                                 onerror="this.src='img/default.png';">
                            <div class="auction-info">
                                <span class="status-tag ${statusClass}">${statusText}</span>
                                <div class="auction-title">${auction.productName || '未知商品'}</div>
                                <div class="price-info">
                                    <span class="current-price">￥${auction.currentPrice || 0}</span>
                                    <span class="original-price">￥${auction.originalPrice || 0}</span>
                                </div>
                                <div class="time-info">
                                    <i class="far fa-clock"></i>
                                    <span>${formatTimeLeft(auction.endTime)}</span>
                                    <span class="bid-count">${auction.bidCount || 0}次出价</span>
                                </div>
                                <div class="location">
                                    <i class="fas fa-map-marker-alt"></i>
                                    <span>${auction.location ? JSON.parse(auction.location).province : '暂无地址'}</span>
                                </div>
                            </div>
                        </div>
                    `;
                    console.log('生成的卡片HTML:', card);
                    container.append(card);
                } catch (error) {
                    console.error('渲染商品卡片失败:', error, auction);
                }
            });
        }

        // 获取拍卖状态
        function getAuctionStatus(startTime, endTime) {
            const now = new Date().getTime();
            const start = new Date(startTime).getTime();
            const end = new Date(endTime).getTime();

            console.log('时间比较:', {
                now: new Date(now),
                start: new Date(start),
                end: new Date(end)
            });

            if (now < start) return 'upcoming';
            if (now > end) return 'ended';
            return 'ongoing';
        }

        // 获取状态样式类
        function getStatusClass(status) {
            return {
                'upcoming': 'status-upcoming',
                'ongoing': 'status-ongoing',
                'ended': 'status-ended'
            }[status];
        }

        // 获取状态文本
        function getStatusText(status) {
            return {
                'upcoming': '即将开始',
                'ongoing': '正在进行',
                'ended': '已结束'
            }[status];
        }

        // 格式化剩余时间
        function formatTimeLeft(endTime) {
            const end = new Date(endTime).getTime();
            const now = new Date().getTime();
            const diff = end - now;

            if (diff <= 0) return '已结束';

            const days = Math.floor(diff / (1000 * 60 * 60 * 24));
            const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));

            if (days > 0) return `${days}天${hours}小时`;
            if (hours > 0) return `${hours}小时${minutes}分`;
            return `${minutes}分钟`;
        }

        // 页面加载时初始化
        $(document).ready(function() {
            loadAuctions();
        });
    </script>
</body>
</html> 